﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Configuration;
using System.Data;
using Discuz.Common;

namespace DingTalkAgent.Web.Common
{
    /// <summary>
    /// 用户平台端Controller基类 
    /// 可将公共数据或企业账户数据
    /// 
    /// 约定：（用户平台，对角色进行按用户模块权限划分。【其中通讯录为公众模块】）
    /// 为方便应该权限过滤，开发者为每个应用模块创建Controller，且保证同一应用不出现两Controller
    /// </summary>
    public class adminBaseController : Controller
    {
        /// <summary>
        /// 企业管理平台，登录者账户信息
        /// </summary>
        protected DingTalkAgent.Model._view_Admin_Info _view_Admin_Info;
        //会话状态获取
        protected override void Initialize(System.Web.Routing.RequestContext requestContext)
        {
            // _view_account = new Model._view_Admin_Info() { ID=1,QY_ID=0,Name="admin", UserID=1};
            if (requestContext.HttpContext.Session["admin"] == null)
            {
                if (requestContext.HttpContext.Request.IsAjaxRequest())
                {
                    requestContext.HttpContext.Response.Write("CS00");
                    requestContext.HttpContext.Response.End();
                    return;
                }
                else
                {
                    requestContext.HttpContext.Response.Redirect("~/Account/LogOn");
                    return;
                }

            }
            else
            {
                _view_Admin_Info = (Model._view_Admin_Info)requestContext.HttpContext.Session["admin"];
            }

            var actionName = requestContext.RouteData.Values["action"].ToString().ToLower(); ;
            ViewBag.action = actionName;
            base.Initialize(requestContext);
        }

        /// <summary>
        /// NPOI.dll方法导出Excel
        /// </summary>
        /// <param name="table">datatable</param>
        /// <param name="fileName">文件名称</param>
        /// <param name="strHeaderText">表头文本</param>
        /// <returns></returns>
        [NonAction]
        protected FileContentResult ExportExcelBase(DataTable table, string fileName, string strHeaderText = null)
        {
            string dt = DateTime.Now.ToString("yyyyMMddHHmmss");
            string headerName = string.IsNullOrEmpty(fileName) ? dt : fileName;
            if (string.IsNullOrEmpty(fileName))
            {
                fileName = dt + ".xls";
            }
            else
            {
                fileName = fileName + "-" + dt + ".xls";
            }

            //ie 需要编码
            if (string.Compare(Request.Browser.Browser, "IE", true) == 0 || string.Compare(Request.Browser.Browser, "InternetExplorer", true) == 0)
            {
                fileName = HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8);
            }
            ExcelOp export = new ExcelOp();
            //var export = Common.ExeclExport.ExcelExportManage.Instance
            return File(export.ExportExcelFromDataTable(table, headerName).GetBuffer(),
                "application/vnd.ms-excel", fileName);
        }
    }
}